<?php
class deparCategoryMod extends commonMod{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function index(){
		$this->assign('cat',$this->getCat());
		//dump($this->getCat());
		$this->display();
	}
	
	public function Depindex(){
		$this->display();
	}
	
	public function Posindex(){
		$superCat = $this->model->field('id,pid,name')->table('category')->where('`pid`=0')->select();
		if($superCat){
			$this->assign('superCat', $superCat);
		}else{
			$this->error('请先添加部门！');
		}
		$this->display();
	}
	
	public function addDep(){
		if(!empty($_POST['nameDep'])){
			$data['pid']         = 0;
			$data['name']        = in($_POST['nameDep']);
			$data['description'] = in($_POST['description']);
		}else{
			$this->error('部门名称不能为空！');
		}
		$condition = array('name'=>$data['name']);
		if(parent::checkUniqu($condition,'category')){
			$this->error('部门已经存在！');
		}else{
			$result = $this->model->table('category')->data($data)->insert();
			if($result){
				$this->success('部门添加成功！');
			}else{
				$this->error('添加失败，请返回重试！');
			}	
		}
			
	}
	
	public function addPos(){
		if(empty($_POST['pid'])){
			$this->error('department must');
		}else{
			$data['description'] = in($_POST['description']);
			$data['pid']		 = intval($_POST['pid']);
			$data['name']        = in($_POST['namePos']);
		}
		
		$result = $this->model->table('category')->data($data)->insert();
		if($result){
			$this->success('添加成功！');
		}else{
			$this->error('添加失败，请返回重试！');
		}	
	}
	
	public function edit(){
		if(isset($_GET['id'])){
			$id = intval($_GET['id']);
		}else{
			$this->error('关键参数丢失，请返回重试！');
		}
		$condition = array('id'=>$id);
		$result = $this->model->table('category')->field('id,name,description')->where($condition)->find();
		if($result){
			$this->assign('result', $result);
			$this->display();
		}else{
			$this->error('数据查询失败！');
		}				
	}
	
	public function save(){
		if(!empty($_POST['hiddenid']) && !empty($_POST['name'])){
			$data['description'] = in($_POST['description']);
			$data['name']		 = in($_POST['name']);
			$id			         = intval($_POST['hiddenid']);
		}else{
			$this->error('参数丢失！');
		}
		$condition = array('name'=>$data['name']);
		if(parent::checkUniqu($condition, 'category')){
			$this->error('栏目已经存在！');
		}else{
			unset($condition);
			$condition = array('id'=>$id);
			$result = $this->model->table('category')->data($data)->where($condition)->update();
			if($result){
				$this->success('更新成功！');
			}else{
				$this->error('更新失败！');
			}
		}	
	}
	
	public function del(){
		if(!empty($_GET['id'])){
			$id = intval($_GET['id']);
		}else{
			$this->error('未知错误');
		}
		$condition = array();
		$condition['pid']=$id;
		if($this->model->table('category')->where($condition)->count()){
			$this->error('请先删除该部门下的职位！');
		}
		unset($condition);
		$condition['id'] = $id;
		$delInfo = $this->model->table('category')->where($condition)->delete();
		if($delInfo){
			$this->success('删除成功！');
		}else{
			$this->error('删除失败！');
		}
	}
}

